﻿using System;
using System.Web;
using CatClient.Context;
using PsCat;
using Newtonsoft.Json;
using PostSharp.Aspects;
using CatClient;
using System.Web.Routing;
using Owin;
using System.Collections.Generic;

namespace PsCat
{
	// Token: 0x02000008 RID: 8
	public class PostsharpEntryTranProcesser : IEntryTranProcesser
	{
		// Token: 0x0600001D RID: 29 RVA: 0x00002623 File Offset: 0x00000823
		public PostsharpEntryTranProcesser(ICatAttributeExtension catAttr, MethodExecutionArgs args, string name, string desc)
		{
			this._catAttr = catAttr;
			this._args = args;
			this._name = name;
			this._desc = desc;
		}

		// Token: 0x0600001E RID: 30 RVA: 0x00002648 File Offset: 0x00000848
		public string GetName(EntryTranNameType type)
		{
			string text = this._name;
			string text2 = this._desc;
			if (type == EntryTranNameType.MethodCall)
			{
				if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
				{
					Type declaringType = this._args.Method.DeclaringType;
					text = (((declaringType != null) ? declaringType.Name : null) ?? (string.IsNullOrEmpty(text) ? "Anonymous" : text));
					text2 = this._args.Method.Name;
				}
				return string.Format("{0}.{1}", text, text2);
			}
			if (type != EntryTranNameType.PigeonCall)
			{
				if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
				{
					if (this._args.Method.DeclaringType == null)
					{
						string text3;
						if (type != EntryTranNameType.UrlCall)
						{
							text3 = string.Format("/Anonymous/{0}", this._args.Method.Name);
						}
						else
						{
							HttpContext httpContext = HttpContext.Current;
							text3 = ((httpContext != null) ? httpContext.Request.Url.AbsolutePath : null);
						}
						text = text3;
					}
					else
					{
						text = string.Format("/{0}/{1}", this._args.Method.DeclaringType.Name, this._args.Method.Name);
					}
				}
				else
				{
					text = string.Format("/{0}/{1}", text, text2);
				}
				return text;
			}
			if (!string.IsNullOrEmpty(text) || !string.IsNullOrEmpty(text2))
			{
				return text + "." + text2;
			}
			return this._catAttr.NameFormClassMethod(this._args);
		}

		// Token: 0x0600001F RID: 31 RVA: 0x000027A6 File Offset: 0x000009A6
		public string GetClientIp()
		{
			return CatCoreUtils.GetClientIp();
		}

		// Token: 0x06000020 RID: 32 RVA: 0x000027AD File Offset: 0x000009AD
		public string GetMethodArgs()
		{
			return this._catAttr.MethodParams(this._args);
		}

		// Token: 0x06000021 RID: 33 RVA: 0x000027C0 File Offset: 0x000009C0
		public string GetRequestArgs()
		{
			return this._catAttr.HttpContextParams(this._args);
		}

        // Token: 0x06000022 RID: 34 RVA: 0x000027D4 File Offset: 0x000009D4
        public CatContext GetCatContext()
        {
            if (HttpContext.Current != null)
            {
                string text = HttpContext.Current.Request.Headers["catContext"];
                if (!string.IsNullOrEmpty(text))
                {
                    try
                    {
                        return JsonConvert.DeserializeObject<CatContext>(text);
                    }
                    catch (Exception ex)
                    {
                        Cat.LogError(ex);
                    }
                }
            }
            else if (OwinRequestScopeContext.Current != null)
            {
                var env = (IDictionary<string, object>)OwinRequestScopeContext.Current.Environment;
                if (env.TryGetValue("owin.RequestHeaders",out object header))
                {
                    if (((IDictionary<string, string[]>)header).TryGetValue("catContext",out string[] catContext))
                    {
                        var text = catContext[0];
                        if (!string.IsNullOrEmpty(text))
                        {
                            try
                            {
                                return JsonConvert.DeserializeObject<CatContext>(text);
                            }
                            catch (Exception ex)
                            {
                                Cat.LogError(ex);
                            }
                        }
                    }
                }
            }
            return null;
        }

		// Token: 0x0400000A RID: 10
		private readonly ICatAttributeExtension _catAttr;

		// Token: 0x0400000B RID: 11
		private readonly MethodExecutionArgs _args;

		// Token: 0x0400000C RID: 12
		private readonly string _name;

		// Token: 0x0400000D RID: 13
		private readonly string _desc;
	}
}
